// interrogation/subscribe/subscribe.js
const call = require('../../utils/request.js');
let doctorId = null; //医生ID
Page({

  /**
   * 页面的初始数据
   */
  data: {
    days:[],  //预约的日期
    dayIndex: 0, //选择日期
    times1:[], //预约的具体时间 上午
    times11:[], //补足
    time1Index: null, //选择时间
    times2:[], //预约的具体时间 下午
    times22:[], //补足
    time2Index: null, //选择时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.doctorId = options.id;
    this.getYyTime();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //获取预约时间
  getYyTime(time){
    call.post('/api/camera/booking_date_List',{
      doctor_id:this.doctorId,
      date_time:time||'',
    }).then(res=>{
      console.log("获取医生预约时间",res)
      try{
        const days = res.data.data.seven_day_date.map((item,index,arr)=>{
          return {
            value:item,
            day:item.split('-').slice(1,3).join('-'),
            week:this.getWeekByDay(item)
          };
        });
        let times1 = [],times2 = [];
        for(let i of res.data.data.date_time_list){
          const timeArr = i.date_time.split('-');
          const strArr = timeArr[1].split(":")
          if((parseInt(strArr[0])==12&&parseInt(strArr[1])>0) || parseInt(strArr[0])>12){
            times2.push(i)
          }else{
            times1.push(i)
          }
        }

        this.setData({
          days: days,
          times1,
          times11: 3 % (times1.length % 3),
          times2,
          times22: 3 % (times2.length % 3),
        })
      }catch(err){}
    })
  },

  //选择日期
  chooseDay(e){
    const index = e.currentTarget.dataset.index;
    this.setData({
      dayIndex: index
    })
    this.getYyTime(this.data.days[index].value)
  },
  
  //选择时间
  chooseTime(e){
    const index = e.currentTarget.dataset.index;
    const status = e.currentTarget.dataset.status;
    const type = e.currentTarget.dataset.type;
    const type2 = type==1?2:1;
    if(status==0){
      this.setData({
        [`time${type}Index`]:index,
        [`time${type2}Index`]:null,
      })
    }
  },

  //立即预约 
  toPay(){
    const time1Index = this.data.time1Index;
    const time2Index = this.data.time2Index;
    
    console.log(time1Index,time2Index);
    if(time1Index || time2Index){
      let time = time1Index?this.data.times1[time1Index].date_time:this.data.times2[time2Index].date_time;
      let timeArr = time.split('-')
      const day = this.data.days[this.data.dayIndex].value;
      
      wx.setStorageSync('yyTime', JSON.stringify({
        id: this.doctorId,
        start_time: [day,timeArr[0]].join(' '),
        end_time: [day,timeArr[1]].join(' '),
      }))
      wx.navigateTo({
        url: `../../interrogation/buy/buy?type=3`,
      })
    }else{
      wx.showToast({
        title: '请选择时间',
        icon:'none'
      })
    }
  },

  //获取日期对应的星期
  getWeekByDay(day){
    day = day.replace(/-/g,"/");
    const timer = new Date(day);
    switch(timer.getDay()){
      case 0: return '周日';
      case 1: return '周一';
      case 2: return '周二';
      case 3: return '周三';
      case 4: return '周四';
      case 5: return '周五';
      case 6: return '周六';
    }
  }
})